﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Common.ValueObject;

namespace ERP_RORZE_TOOL.DBAccess
{
	class ToolStatusDAO : BaseDAO
	{
		#region Basic Method

		public static DataTable GetAll()
		{
			return ExcuteProcedure(ToolConstants.StoreProcedure.ToolStatus.GetAll);
		}

		public static DataTable GetOne(int toolStatusId)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
						CreateParameter("@StatusID", SqlDbType.Int, toolStatusId)
                    };
				return ExcuteProcedure(ToolConstants.StoreProcedure.ToolStatus.GetOne, parameters);

			}
			catch(Exception e)
			{
				throw new MyException(e);
			}

		}

		public static ToolStatus LoadOne(int toolStatusId)
		{
			ToolStatus o = new ToolStatus();
			DataTable dt = GetOne(toolStatusId);
			if(dt.Rows.Count > 0)
			{
				o.StatusID = int.Parse(dt.Rows[0][ToolConstants.Entities.ToolStatus.FieldName.StatusID].ToString());
				o.StatusName = dt.Rows[0][ToolConstants.Entities.ToolStatus.FieldName.StatusName].ToString();

			}

			return o;
		}


		public static int Insert(ToolStatus o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
                        CreateParameter("@StatusName", SqlDbType.NVarChar, o.StatusName),
                    };

				return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.ToolStatus.Insert, parameters);

			}
			catch(Exception e)
			{
				throw new MyException(e);
			}

		}

		public static int Update(ToolStatus o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
						 CreateParameter("@StatusID", SqlDbType.Int, o.StatusID),
                        CreateParameter("@StatusName", SqlDbType.NVarChar, o.StatusName),
                    };

				return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.ToolStatus.Update, parameters);
			}
			catch(Exception e)
			{
				throw new MyException(e);
			}
		}

		public static int Delete(ToolStatus o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
                        CreateParameter("@StatusID", SqlDbType.Int, o.StatusID)
                    };

				return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.ToolStatus.Delete, parameters);
			}
			catch(Exception e)
			{
				throw new MyException(e);
			}
		}

		public static DataTable GetTemplateTable()
		{
			DataTable table = new DataTable("333_ToolStatus");

			return table;
		}

		#endregion
	}
}
